import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {
    List,
    Categories,
} from './DTO';
const baseUrl = '/afterSales/api/v1';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '故障原因'
});

const listDesc = formatMessage({
    id: 'api.message.listDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增{resourceName}'
}, {resourceName});

const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废{resourceName}'
}, {resourceName});

const exportDesc = formatMessage({
    id: 'api.exportDesc',
    defaultMessage: '导出{resourceName}'
}, {resourceName});

const resourceName2 = formatMessage({
    id: 'api.resourceName2',
    defaultMessage: '故障原因分类'
});

const categoryDesc = formatMessage({
    id: 'api.message.categoryDesc',
    defaultMessage: '查询{resourceName2}'
}, {resourceName2});

const addCategoryDesc = formatMessage({
    id: 'api.message.addCategoryDesc',
    defaultMessage: '新增{resourceName2}'
}, {resourceName2});

const updateCategoryDesc = formatMessage({
    id: 'api.message.updateCategoryDesc',
    defaultMessage: '编辑{resourceName2}'
}, {resourceName2});

const abandonCategoryDesc = formatMessage({
    id: 'api.message.abandonCategoryDesc',
    defaultMessage: '作废{resourceName2}'
}, {resourceName2});

// 查询
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/faultModes/forCurrentUser?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, listDesc));
};


// 新增
export const addInfo = data => request(`${baseUrl}/faultModes`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));

// 编辑
export const updateInfo = (id, data) => request(`${baseUrl}/faultModes/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 作废
export const abandonInfo = (id, data) => request(`${baseUrl}/faultModes/${id}/abandon`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));

// 导出
export const exportInfo = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/faultModes/export/forCurrentUser?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};

// 查询顶层分类
export const getCategoriesTop = () => request(`${baseUrl}/faultModeCategories/topLevel/forCurrentUser`, null, Categories)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, categoryDesc));

// 查询分类
export const getCategories = id => request(`${baseUrl}/faultModeCategories/${id}/children`, null, Categories)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, categoryDesc));


// 新增分类
export const addCategory = data => request(`${baseUrl}/faultModeCategories`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addCategoryDesc));

// 编辑分类
export const updateCategory = (id, data) => request(`${baseUrl}/faultModeCategories/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateCategoryDesc));

// 作废分类
export const abandonCategory = (id, data) => request(`${baseUrl}/faultModeCategories/${id}/abandon`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonCategoryDesc));
